package com.biyabi.service.impl;

import com.biyabi.dao.SupplierMapper;
import com.biyabi.entity.Supplier;
import com.biyabi.entity.SupplierExample;
import com.biyabi.pojo.SuppliersData;
import com.biyabi.service.SupplierService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public SuppliersData findAll(Integer currentPage, Integer pageSize,String columnName,String columnValue){
        currentPage = currentPage == null ? 1:currentPage;
        pageSize= pageSize== null ? 5:pageSize;
        SupplierExample supplierExample = new SupplierExample();
        if(columnName!=null && columnValue!=null){
            switch (columnName){
                case "name":
                    supplierExample.createCriteria().andNameLike("%"+columnValue+"%");
                    break;
            }
        }
        SuppliersData suppliersData = new SuppliersData();
        suppliersData .setTotal(supplierMapper.countByExample(supplierExample));
        PageHelper.startPage(currentPage,pageSize);
        List<Supplier> brands = supplierMapper.selectByExample(supplierExample);
        suppliersData .setRows(brands);
        return suppliersData ;
    }
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Supplier> findAllSupplier(){
        return supplierMapper.selectByExample(new SupplierExample());
    }
    public void add(Supplier supplier, MultipartFile logoFile, HttpServletRequest request){
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/upload/logo");
            supplier.setId(UUID.randomUUID().toString());
            supplier.setLogo("upload/logo/"+logoFile.getOriginalFilename());
            supplierMapper.insert(supplier);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
    public void update(Supplier supplier, MultipartFile logoFile, HttpServletRequest request){
        try {
            if(!logoFile.isEmpty()){
                String realPath = request.getSession().getServletContext().getRealPath("/upload/logo");
                Supplier supplier1 = supplierMapper.selectByPrimaryKey(supplier.getId());
                String logoRealPath = request.getSession().getServletContext().getRealPath(supplier1.getLogo());
                File file = new File(logoRealPath);
                if(file.exists()){
                    file.delete();
                }
                supplier.setLogo("upload/logo/"+logoFile.getOriginalFilename());
            }
            supplierMapper.updateByPrimaryKey(supplier);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Supplier findOne(String id) {
        return supplierMapper.selectByPrimaryKey(id);
    }
}